package com.example.beat_x3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.Fragments.BackgroundFragment;

import com.example.beat_x3.Fragments.HomeFragment;
import com.example.beat_x3.Fragments.InstrumentFragment;
import com.example.beat_x3.Fragments.UserFragment;
import com.example.beat_x3.Fragments.VenueFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import Entity.Inf;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Inf.context = this;
        LoadImages.setContext(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Inf.place_images = LoadImages.init(LoadImages.PLACE_IMAGES);
                Inf.equipment_images = LoadImages.init(LoadImages.EQUIPMENT_IMAGES);
            }
        }).start();
        Inf.bcFragment = new BackgroundFragment();
        Inf.homeFragment = new HomeFragment();
        Inf.venueFragment = new VenueFragment();
        Inf.instrumentFragment = new InstrumentFragment();

        Inf.current_fragment = Inf.bcFragment;
        changeFragment(Inf.homeFragment);
        BottomNavigationView menu = findViewById(R.id.bnv1);
        menu.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        changeFragment(Inf.homeFragment);
                        break;
                    case R.id.venue:
                        if (!Inf.placeSchedule_in) changeFragment(Inf.venueFragment);
                        else changeFragment(Inf.placeScheduleFragment);
                        break;
                    case R.id.instrument:
                        changeFragment(Inf.instrumentFragment);
                        break;
                    case R.id.user:
                        if (Inf.userFragment == null) Inf.userFragment = new UserFragment();
                        if (Inf.isLog) changeFragment(Inf.centerFragment);
                        else changeFragment(Inf.userFragment);
                        break;
                }
                return true;
            }
        });
    }

    public void changeFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (Inf.current_fragment != fragment) {
            if (!fragment.isAdded()) {
                transaction.hide(Inf.current_fragment).add(R.id.fragmentContainerView, fragment).commit();
            } else {
                transaction.hide(Inf.current_fragment).show(fragment).commit();
            }
            Inf.current_fragment = fragment;
        }
    }

    private boolean isBackPressed = false;

    @Override
    public void onBackPressed() {
        if (isBackPressed) {
            Inf.context = null;
            finish();
            super.onBackPressed();
        } else {
            isBackPressed = true;
            Toast.makeText(this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    isBackPressed = false;
                }
            }, 2000);
        }
    }
}